package gui;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import algorithm.LocalAlignment;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Label;

import utils.Constant;

public class MainWindow {

	//static Composite composite;
	static Display display;
	static LocalAlignment l;
	private static Text text_threshhold;
	private static Label txtThreshhold;
	private static Label text_largest_score;
	static Canvas canvas;
	//	static float pix_x,pix_y;
	static int opt_run=0,sub_run=0;

	static int threshhold;
	private static Label text_x;
	private static Label text_y;
	private static Label txtMatch;
	private static Label txtMismatch;
	private static Label txtGapPenalty;
	private static Label txtGapExtend;
	private static Text txtmatchscore;
	private static Text txtmismatchscore;
	private static Text txtgappenaltyscore;
	private static Text txtgapextendscore;
	static Button btnGapAffine;
	private static Text str1_content;
	private static Text str2_content;
	private static Label lblScoring;
	
	
	
	static 	int linewidth = 2;
	static Composite composite;
	static int win_x = 960;
	static int win_y = 700;
	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
//		String str1 = "ATCAGAGTC";
//		String str2 = "GTCAGTCA";
		
//		String str1 = "migtdprtilkdllpetipppelddmtlwqivinilseppkrkkrkdintiedavkllqeckkiivltgagvsvscgipdfrsrdgiyarlavdfpdlpdpqamfdieyfrkdprpffkfakeiypgqfqpslchkfialsdkegkllrnytqnidtleqvagiqriiqchgsfatasclickykvdceavrgalfsqvvprcprcpadeplaimkpeivffgenlpeqfhramkydkdevdllivigsslkvrpvalipssiphevpqilinreplphlhfdvellgdcdviinelchrlggeyaklccnpvklseitekpprtqkelaylselpptplhvsedssspertsppdssvivtlldqaaksnddldvseskgcmeekpqevqtsrnvesiaeqmenpdlknvgsstgeknertsvagtvrkcwpnrvakeqisrrldgnqylflppnryifhgaevysdseddvlsssscgsnsdsgtcqspsleepmedeseieefyngledepdvperaggagfgtdgddqeaineaisvkqevtdmnypsnks";
//		String str2 = "maaaaaaaaigyrgpytfvqqhlmigtdprtilkdllpetipppelddmtlwqivinilseppkrkkrkdintiedavkllqeckkiivltgagvsvscgipdfrsrdgiyarlavdfpdlpdpqamfdieyfrkdprpffkfakeiypgqfqpslchkfialsdkegkllrnytqnidtleqvagiqrilqchgsfatasclickykvdceavrgdifnqvvprcprcpadeplaimkpeivffgenlpeqfhramkydkdevdllivigsslkvrpvalipssiphevpqilinreplphlhfdvellgdcdviinelchrlggeyaklccnpvklseitekpprpqkelvhlselpptplhisedssspertvpqdssviatlvdqatnnnvndlevsesscveekpqevqtsrnveninvenpdfkavgsstadknertsvaetvrkcwpnrlakeqiskrlegnqylfvppnryifhgaevysdseddvlsssscgsnsdsgtcqspsleepledeseieefyngleddterpecaggsgfgadggdqevvneaiatrqeltdvnypsdks";		
		
		String str1 = "CCTGGGGCAGATGCTCGATCCACTGAAGGAGCGCATCCAGTGCGTTCGAGAAGCGGGTGGAGGAAAGCTACCAGCAGGAAGCGCGCGAGCGTTTCTCCCTGAGCAAGGAACTGGAGCGTCTGCAGCAGCTCAACCTGCGCCTGGGCGAGGAAGCGACCAACCTGACCCGCGCGCTCAAGGGCCAGAAGACCCAGGGCAACTGGGGCGAACTGGTGCTGGAGCGGGTGCTCGAGCATGCAGGCCTGGAAAAAGGCCGGGAATACGAGACCCAGGTCAGCCTGAAGGGTGCCGAGGGCGAACGCTTCCAGCCGGACGTGCTGATCCGCCTGCCCGGCGACAAGCAGGTGGTGGTGGATGCCAAGGTCAGTCTCACGGCCTACCAGCAGTACATCGCCGCTGACGACGATCTGCTGCGCCAGCAGGCGCTGAAGCAGCACGTAACGTCGTTGCGCAACCATGTGAAAGGCCTCTCCGGCAAGGACTATCGACGCCTGGAAGGATTGCACAGCCTCGACTTCGTCCTGCTGTTCATGCCGATCGAGGCGGCTTTCTCGACGGCCCTGCAGGCCGAGCCGAACCTGTTCCAGGACGCCTTCGCGCAGAATATCGTGATCGTCAGCCCGACCACCCTGCTGGCCACCCTGCGGGTAATCGACAGCCTCTGGCGCCAGGAGCGGCAGAACCAGAATGCGCGGGAAATCGCCGAACGTGCCGGTTCGCTGTATGACAAGTTCGTGCTCTTCGTCCAGGACCTCGATGAGGTTGGTTCGCGCCTGCAGCAACTGGACAAGGCCTATGCCGCGGCGCGCAACAAGCTCACCGAGGGACGTGGCAACCTGATCAGTCGCAGCGAGCAACTCAAGCTGCTCGGTGCCCGCGCCAGCAAGAACCTGCCGACGGACTGGCTGGACCGGGCGCTGGTGGCGCCCGTGGAAGAGGCCGGAGAGGAGTGA";
		String str2 = "ATGCCCCGACCACTTTCCCACCGGAACTTGCCCATGACGCCCCAAGGCCACTGTGACAGCCGTTTCGCCCCGCTTGCCGAGGCCTTCGCCCGGCTTTTCGACAATCCGCAGGAGCGCGGCGCGGCGTTGTGCCTGGAGGTCGGTGGCGAGACGGTGGTCGACCTCTGGGGCGGCGTCGCGGACAAGGACGGCGAGCAGCCCTGGCAGCGCGACACCATCCTCAATCTGTTCTCCTGCACCAAGACCTTCACCGCCGTTGCCGTGCTGCAACTGGTGGCCGAGGGCAAGCTCGAACTGGACGCGCCGGTGGCACACTACTGGCCCGAGTTCGCCGCGGCCGGCAAGGCAGCGATCAGCGTGCGCCAGTTGCTCTGCCATCGCGCCGGCCTGCCGGCGCTGCGCGAGCAGATGCCGCCCGAGGCACTCTACGACTGGCAGGCGATGACCACGGCACTGGCCGCCGAGGAGCCCTGGTGGACGCCTGGCGAAGCGCACGGCTATGCACCGATTACCTATGGCTGGCTGCTCGGCGAGGTGATCCGCCGGGTCGACGGCCGCGAGCCGGGCGAGGCCATCGTGGCGCGCACCGCGGCACCGCTGGGCCTGGATTTCCATGTCGGCCTGGACGACAGCCAGTTCGGCCGCGTCGCCCACATGGCACGGAGCAAGGGCAGCCTCGGCGACGCTGCCGCCCAGCGCATGCTGAAGACCATGATGAGCGAGCCATTGGCGCTGACCACCCGTGCCTTCACCAATCCGCCGTCGATCCTCACCAGCACCAACAAGCCGGAATGGCGACGCATGCAGCAGCCGGCCGCCAATGGCCATGGCAACGCGCGCGCGCTCGCCGGCTTCTATAGCGGCCTGTTGCAGGGACGTTTGCTGGAAGATGAACTGCTGGGCCAGTTGGCCCACGAACACGCGGTAGGCGAGGATCGGACCTTGCTGACCCGGACCCGCTTCGGCCTCGGCACCATGCTCGACCAACCGGGCGTGGCCAATGCGACCTACGGACTCGGCCCCAAGGCCTTCGGCCACCCTGGGGCGGGTGGCTCGATCGGCTTCGCCGATCCCGAGCGCGACCTGGCTTTTTCCTTTGTCAGCAACGCCTTGGGGCCGTATGTGCTGATGGATCCGCGGGCGCAGCAGCTGGCGCGTGTCGCGGGGGAGTGTCTGTAA";
		int x = 20;
		int y = 20;
		
		int use_x = 200;
		int use_y = 600;
		
		int text_height=20;

		//shell
		display = Display.getDefault();
		Shell shell = new Shell();
		shell.setSize(win_x + use_x + 3*x, win_y + 4*y);
		shell.setText("SWT Application");
		
		//user interact composite
		Composite user_composite = new Composite(shell, SWT.NONE);
		user_composite.setBounds(20, 20, 202, 700);
		// Run optimal Button
		Button btnOptimal = new Button(user_composite, SWT.NONE);
		btnOptimal.setBounds(10, 10, 180, 30);
		btnOptimal.setText("Optimal Alignment");
		btnOptimal.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent arg0) {
				opt_run=1;
				utils.Constant.match = Integer.valueOf(txtmatchscore.getText());
				utils.Constant.mismatch = Integer.valueOf(txtmismatchscore.getText());
				utils.Constant.indel = Integer.valueOf(txtgappenaltyscore.getText());
				utils.Constant.extend = Integer.valueOf(txtgapextendscore.getText());
				
				l.setStr1(str1_content.getText());
				l.setStr2(str2_content.getText());
				//int strlen = str1_content.getText().length()<str2_content.getText().length()?str1_content.getText().length():str2_content.getText().length();
				int tmp_pix_x = 1;
				int strlen = str1_content.getText().length();
				while((tmp_pix_x+1) * strlen < win_x-20)
				{
					tmp_pix_x++;
				}
				int tmp_pix_y=1;
				strlen = str2_content.getText().length();
				while((tmp_pix_y+1) * strlen < win_y-20)
				{
					tmp_pix_y++;
				}
				utils.Constant.pixel = tmp_pix_x<=tmp_pix_y?tmp_pix_x:tmp_pix_y;
				
				text_x.setText("click alignments to see details:");
//				text_x.setBounds(20, 0, str1_content.getText().length()*utils.Constant.pixel, 20);
//				text_x.setText(str1_content.getText());
//				text_y.setBounds(0, 20, 20, str2_content.getText().length()*utils.Constant.pixel);
//				text_y.setText(str2_content.getText());
				
			    composite.setSize(20+str1_content.getText().length()*utils.Constant.pixel, 20+str2_content.getText().length()*utils.Constant.pixel);
				l.opt_alignment();
				l.backtracking();
				threshhold = (int)(l.largest * 0.9);
				String str_Largest = new String ("Largest Score: "+ String.valueOf(l.largest));
				text_largest_score.setText(str_Largest);
				text_threshhold.setText(String.valueOf(threshhold));
				
				GC gc = new GC(canvas);
				gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
				gc.setLineWidth(linewidth);
				int x1,y1,x2,y2;
				ArrayList<List> opt_alignments = l.getOpt_alignments();
				for(List<Integer> list_int: opt_alignments){
					int[] tmp= l.mapping_1To2(list_int.get(0));
					x1 = tmp[0];
					y1 = tmp[1];
					for(int i=0;i<list_int.size()-1;i++){
						tmp = l.mapping_1To2(list_int.get(i+1));
						x2 = tmp[0];
						y2 = tmp[1];
						if(!(x1-x2==1&&y1-y2==1))
						{
							gc.setLineWidth(linewidth*3);
							gc.setForeground(display.getSystemColor(SWT.COLOR_DARK_GREEN));
						}
						else if(str1_content.getText().charAt(y1-1)!=str2_content.getText().charAt(x1-1))
						{
							gc.setLineWidth(linewidth*3);
							gc.setForeground(display.getSystemColor(SWT.COLOR_DARK_RED));
						}
						gc.drawLine( y1*utils.Constant.pixel, x1*utils.Constant.pixel, y2*utils.Constant.pixel, x2*utils.Constant.pixel);						
						x1 = x2;
						y1 = y2;
						gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
						gc.setLineWidth(linewidth);
					}
				}	
				gc.dispose();
			}
		});
		// run Sub-Optimal Button
		Button btnSuboptimalAlignment = new Button(user_composite, SWT.NONE);
		btnSuboptimalAlignment.setText("Sub-Optimal Alignment");
		btnSuboptimalAlignment.setBounds(10, 65, 180, 30);
		btnSuboptimalAlignment.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent arg0) {
				sub_run=1;
				String tem = text_threshhold.getText();
				if(!tem.isEmpty()){
					threshhold = Integer.valueOf(tem);
				}		
				l.setThreshhold(threshhold);
				l.sub_alignment();
				l.sub_backtrack();
				//graph
				GC gc = new GC(canvas);
				int x1,y1,x2,y2;
				ArrayList<List> sub_alignments = l.getSub_alignments();
				gc.setForeground(display.getSystemColor(SWT.COLOR_CYAN));
				gc.setLineWidth(linewidth);
				for(List<Integer> list_int: sub_alignments){
					int[] tmp= l.mapping_1To2(list_int.get(0));
					x1 = tmp[0];
					y1 = tmp[1];
					for(int i=0;i<list_int.size()-1;i++){
						tmp = l.mapping_1To2(list_int.get(i+1));
						x2 = tmp[0];
						y2 = tmp[1];
						if(!(x1-x2==1&&y1-y2==1))
						{
							gc.setLineWidth(linewidth*3);
							gc.setForeground(display.getSystemColor(SWT.COLOR_DARK_GREEN));
						}
						else if(str1_content.getText().charAt(y1-1)!=str2_content.getText().charAt(x1-1))
						{
							gc.setLineWidth(linewidth*3);
							gc.setForeground(display.getSystemColor(SWT.COLOR_DARK_RED));
						}
						gc.drawLine( y1*utils.Constant.pixel, x1*utils.Constant.pixel, y2*utils.Constant.pixel, x2*utils.Constant.pixel);
						x1 = x2;
						y1 = y2;
						gc.setForeground(display.getSystemColor(SWT.COLOR_CYAN));
						gc.setLineWidth(linewidth);
					}
				}
				gc.dispose();
			}
		});
		//
		text_threshhold = new Text(user_composite, SWT.BORDER);
		text_threshhold.setBounds(10, 125, 160, text_height);

		txtThreshhold = new Label(user_composite, SWT.BORDER | SWT.READ_ONLY);
		txtThreshhold.setText("Threshhold:");
		txtThreshhold.setBounds(10, 100, 160, text_height);

		text_largest_score = new Label(user_composite, SWT.BORDER);
		text_largest_score.setBounds(10, 40, 160, 20);
		
		btnGapAffine = new Button(user_composite, SWT.CHECK);
		btnGapAffine.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				if(btnGapAffine.getSelection()==true){
					utils.Constant.isAffineGapScore=true;
					txtgapextendscore.setVisible(true);
					txtGapExtend.setVisible(true);
					txtGapPenalty.setText("Gap Penalty:");
				}
				else
				{
					utils.Constant.isAffineGapScore=false;
					txtgapextendscore.setVisible(false);
					txtGapExtend.setVisible(false);
					txtGapPenalty.setText("InDel:");
				}
			}
		});
		btnGapAffine.setBounds(99, 172, 91, 18);
		btnGapAffine.setText("Gap Affine");
		btnGapAffine.setSelection(true);
		
		
		txtMatch = new Label(user_composite, SWT.BORDER | SWT.READ_ONLY);
		txtMatch.setText("Match:");
		txtMatch.setBounds(10, 200, 80, text_height);
		
		txtMismatch = new Label(user_composite, SWT.BORDER | SWT.READ_ONLY);
		txtMismatch.setText("MisMatch:");
		txtMismatch.setBounds(10, 225, 80, text_height);
		
		txtmatchscore = new Text(user_composite, SWT.BORDER);
		txtmatchscore.setBounds(106, 200, 64, text_height);
		txtmatchscore.setText(String.valueOf(utils.Constant.match));
		
		txtmismatchscore = new Text(user_composite, SWT.BORDER);
		txtmismatchscore.setBounds(106, 226, 64, text_height);
		txtmismatchscore.setText(String.valueOf(utils.Constant.mismatch));
		
		txtGapPenalty = new Label(user_composite, SWT.BORDER | SWT.READ_ONLY);
		txtGapPenalty.setText("Gap Penalty:");
		txtGapPenalty.setBounds(10, 250, 80, text_height);

		txtGapExtend = new Label(user_composite, SWT.BORDER | SWT.READ_ONLY);
		txtGapExtend.setText("Gap Extend:");
		txtGapExtend.setBounds(10, 276, 80, text_height);

		txtgappenaltyscore = new Text(user_composite, SWT.BORDER);
		txtgappenaltyscore.setBounds(106, 251, 64, text_height);
		txtgappenaltyscore.setText(String.valueOf(utils.Constant.indel));

		txtgapextendscore = new Text(user_composite, SWT.BORDER);
		txtgapextendscore.setBounds(106, 276, 64, text_height);
		txtgapextendscore.setText(String.valueOf(utils.Constant.extend));
		
		str1_content = new Text(user_composite, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL );
		str1_content.setBounds(10, 333, 180, 150);
		str1_content.setText(str1);
		
		str2_content = new Text(user_composite, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
		str2_content.setBounds(10, 512, 180, 150);
		str2_content.setText(str2);
		
		Label lblSequence1 = new Label(user_composite, SWT.NONE);
		lblSequence1.setBounds(10, 313, 100, 14);
		lblSequence1.setText("Sequence 1:");
		
		Label lblSequence2 = new Label(user_composite, SWT.NONE);
		lblSequence2.setBounds(10, 489, 100, 14);
		lblSequence2.setText("Sequence 2:");
		
		lblScoring = new Label(user_composite, SWT.NONE);
		lblScoring.setBounds(10, 172, 59, 14);
		lblScoring.setText("Scoring:");

		
		
		// graphic composite
		//***************************************************************************************************************
		ScrolledComposite sc1 = new ScrolledComposite(shell, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
		//composite = new ScrolledComposite(shell, SWT.NONE|SWT.BORDER|SWT.V_SCROLL|SWT.H_SCROLL);
		composite = new Composite (sc1, SWT.NONE);
		sc1.setContent(composite);
		sc1.setBounds(2*x + use_x, y, win_x, win_y);
		//int strlen = str1.length()<str2.length()?str1.length():str2.length();
		int tmp_pix_x = 1;
		int strlen = str1.length();
		while((tmp_pix_x+1) * strlen < win_x-20)
		{
			tmp_pix_x++;
		}
		int tmp_pix_y=1;
		strlen = str2.length();
		while((tmp_pix_y+1) * strlen < win_y-20)
		{
			tmp_pix_y++;
		}
		utils.Constant.pixel = tmp_pix_x<=tmp_pix_y?tmp_pix_x:tmp_pix_y;
//		if(win_x/strlen > 1)
//		{
//			utils.Constant.pixel=win_x/strlen;
//		}
	    composite.setSize(20+str1.length()*utils.Constant.pixel, 20+str2.length()*utils.Constant.pixel);
	    
	    canvas = new Canvas(composite,SWT.NONE);
	    canvas.addMouseListener(new MouseAdapter() {
	    	@Override
	    	public void mouseDown(MouseEvent arg0) {
	    		int y = arg0.x/Constant.pixel;
	    		int x = arg0.y/Constant.pixel;
	    		
	    		Hashtable opt_table = l.getOpt_table();
	    		Hashtable sub_table = l.getSub_table();
	    		
	    		ArrayList<List> opt_alignments = l.getOpt_alignments();
	    		ArrayList<List> sub_alignments = l.getSub_alignments();
	    		ArrayList<String> opt_alignments_str1 = l.getOpt_alignments_str1();
	    		ArrayList<String> opt_alignments_str2 = l.getOpt_alignments_str2();
	    		ArrayList<String> sub_alignments_str1 = l.getSub_alignments_str1();
	    		ArrayList<String> sub_alignments_str2 = l.getSub_alignments_str2();
	    		
	    		List<Integer> oalignments = (List) opt_table.get(l.mapping_2To1(x, y));
	    		if (oalignments!=null){
	    			for(int i:oalignments){
	    				showDetails(opt_alignments.get(i), opt_alignments_str1.get(i), opt_alignments_str2.get(i));
	    			}
	    		}
	    		oalignments = (List) sub_table.get(l.mapping_2To1(x, y));
	    		if (oalignments!=null){
	    			for(int i:oalignments){
	    				showDetails(sub_alignments.get(i), sub_alignments_str1.get(i), sub_alignments_str2.get(i));
	    			}
	    		}
	    		
	    	}

			private void showDetails(List<Integer> list, String str1, String str2) {
				int i = list.get(0);
				int[] tmp= l.mapping_1To2(i);
				int x_end = tmp[0];
				int y_end = tmp[1];
				
				i = list.get(list.size()-1);
				tmp = l.mapping_1To2(i);
				int x_begin = tmp[0];
				int y_begin = tmp[1];
				
				int txt_length = 12;
								
				Shell shell1 = new Shell();
				shell1.open();
				shell1.layout();
				int windowsize = 1200;
				if(str1.length()*txt_length<500)
				{
					windowsize = 500;
				}
				else
				{
					windowsize = 1200<str1.length()*txt_length?1200:str1.length()*txt_length;
				}
				shell1.setSize(windowsize, 150);
				ScrolledComposite sc1 = new ScrolledComposite(shell1, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
				Composite cc1 = new Composite(sc1, SWT.NONE);
				sc1.setContent(cc1);
				sc1.setBounds(0, 0, windowsize, 120);
				cc1.setSize(str1.length()*txt_length>500?str1.length()*txt_length:500, 100);
				ArrayList<Label> txtstr1 = new ArrayList<Label>();
				ArrayList<Label> txtstr2 = new ArrayList<Label>();
				Label labelstr1 = new Label(cc1, SWT.NONE);
	    		labelstr1.setBounds(0, 1, 500, 18);
	    		labelstr1.setText("segment of Sequence 1: (begin position: "+ y_begin + "; end position: " + y_end);
	    		Label labelstr2 = new Label(cc1, SWT.NONE);
	    		labelstr2.setBounds(0, 61, 500, 18);
	    		labelstr2.setText("segment of Sequence 2: (begin position: "+ x_begin + "; end position: " + x_end);
	    		int match=0,mismatch=0,indel=0;
				for(int j = 0; j<str1.length(); j++)
				{
					txtstr1.add(new Label(cc1, SWT.NONE));					
					txtstr1.get(txtstr1.size()-1).setBounds(j*txt_length, 20, txt_length, 20);
					txtstr1.get(txtstr1.size()-1).setText(str1.substring(j, j+1));
					txtstr2.add(new Label(cc1, SWT.NONE));
					txtstr2.get(txtstr2.size()-1).setBounds(j*txt_length, 40, txt_length, 20);
					txtstr2.get(txtstr2.size()-1).setText(str2.substring(j, j+1));
					if(str1.charAt(j)==str2.charAt(j))
					{
						match++;
						txtstr1.get(txtstr1.size()-1).setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
						txtstr2.get(txtstr1.size()-1).setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
					}
					else if (str1.charAt(j)=='_' || str2.charAt(j)=='_')
					{
						indel++;
						txtstr1.get(txtstr1.size()-1).setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
						txtstr2.get(txtstr1.size()-1).setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
					}
					else
					{
						mismatch++;
						txtstr1.get(txtstr1.size()-1).setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
						txtstr2.get(txtstr1.size()-1).setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
					}
				}
				Label labelstr3 = new Label(cc1, SWT.NONE);
	    		labelstr3.setBounds(0, 81, 500, 18);
	    		labelstr3.setText("summary: # of MATCH: "+ match + "; # of indel: " + indel + "; # of mismatch: " + mismatch);
//				System.out.println(str1.length() + "\n" + new_str1 +"\n"+new_str2);
			}
	    });
	    canvas.setBounds(20, 20, str1.length()*utils.Constant.pixel, str2.length()*utils.Constant.pixel);
	    canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
	    
	    text_x = new Label(composite, SWT.BORDER);
	    //text_x.setFont(SWTResourceManager.getFont("Lucida Grande", 5, SWT.NORMAL));
	    text_x.setBackground(SWTResourceManager.getColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));
	    text_x.setBounds(20, 0, str1.length()*utils.Constant.pixel, 20);
	    
	    text_y = new Label(composite, SWT.BORDER | SWT.WRAP);
	    text_y.setBackground(SWTResourceManager.getColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));
	    text_y.setBounds(0, 20, 20, str2.length()*utils.Constant.pixel);
	    
	    canvas.addListener (SWT.Paint, new Listener () {
	        public void handleEvent (Event e) {
	        	if(opt_run==0)
	        		return;
	            GC gc = e.gc;
				gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
				gc.setLineWidth(linewidth);
				int x1,y1,x2,y2;
				ArrayList<List> opt_alignments = l.getOpt_alignments();
				for(List<Integer> list_int: opt_alignments){
					int[] tmp= l.mapping_1To2(list_int.get(0));
					x1 = tmp[0];
					y1 = tmp[1];
					for(int i=0;i<list_int.size()-1;i++){
						tmp = l.mapping_1To2(list_int.get(i+1));
						x2 = tmp[0];
						y2 = tmp[1];
						if(!(x1-x2==1&&y1-y2==1))
						{
							gc.setLineWidth(linewidth*3);
							gc.setForeground(display.getSystemColor(SWT.COLOR_DARK_GREEN));
						}
						else if(str1_content.getText().charAt(y1-1)!=str2_content.getText().charAt(x1-1))
						{
							gc.setLineWidth(linewidth*3);
							gc.setForeground(display.getSystemColor(SWT.COLOR_DARK_RED));
						}
						gc.drawLine( y1*utils.Constant.pixel, x1*utils.Constant.pixel, y2*utils.Constant.pixel, x2*utils.Constant.pixel);						
						x1 = x2;
						y1 = y2;
						gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
						gc.setLineWidth(linewidth);
					}
				}
				if(sub_run==0)
					return;
				ArrayList<List> sub_alignments = l.getSub_alignments();
				gc.setForeground(display.getSystemColor(SWT.COLOR_CYAN));
				gc.setLineWidth(linewidth);
				for(List<Integer> list_int: sub_alignments){
					int[] tmp= l.mapping_1To2(list_int.get(0));
					x1 = tmp[0];
					y1 = tmp[1];
					for(int i=0;i<list_int.size()-1;i++){
						tmp = l.mapping_1To2(list_int.get(i+1));
						x2 = tmp[0];
						y2 = tmp[1];
						if(!(x1-x2==1&&y1-y2==1))
						{
							gc.setLineWidth(linewidth*3);
							gc.setForeground(display.getSystemColor(SWT.COLOR_DARK_GREEN));
						}
						else if(str1_content.getText().charAt(y1-1)!=str2_content.getText().charAt(x1-1))
						{
							gc.setLineWidth(linewidth*3);
							gc.setForeground(display.getSystemColor(SWT.COLOR_DARK_RED));
						}
						gc.drawLine( y1*utils.Constant.pixel, x1*utils.Constant.pixel, y2*utils.Constant.pixel, x2*utils.Constant.pixel);
						x1 = x2;
						y1 = y2;
						gc.setForeground(display.getSystemColor(SWT.COLOR_CYAN));
						gc.setLineWidth(linewidth);
					}
				}
				gc.dispose();
	        }
	    });
		//composite.setBounds(x, y, x+win_x, y+win_y);
	    
	    //get the scoring function

		l = new LocalAlignment();
//		l.setStr1(str1);
//		l.setStr2(str2);
		//		l.opt_alignment();
		//		l.backtracking();
		//		threshhold = l.largest - 5;

		//		l.setThreshhold(threshhold);
		//		l.sub_alignment();
		//		l.sub_backtrack();

		shell.open();
		shell.layout();
	
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}
}
